﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AzukiMap.Map;
using AzukiMap.MapSource;
using Geotools.Geometries;
using AzukiMap.Renderer;
using System.Collections.Generic;

namespace AzukiMap.Layer.Vector
{
    public class LocalVectorLayer : VectorLayer
    {
        LocalVectorSource _vectorSource;

        public LocalVectorLayer(LocalVectorSource vectorSource, MapCanvas canvas, string name) 
            : base((VectorSource)vectorSource, canvas, name)
        {
            _vectorSource = vectorSource;
        }

        public LocalVectorLayer(LocalVectorSource vectorSource, MapCanvas canvas, string name, GTPolygon maxExtent) 
            : base((VectorSource)vectorSource, canvas, name, maxExtent)
        {
            _vectorSource = vectorSource;
        }

        public override void Draw()
        {
            if (_isRendering)
            {
                return;
            }
            if (!_isVisible)
            {
                return;
            }
            if (!CheckScale())
            {
                return;
            }
            _isRendering = true;

            IVectorStyle vStyle;
            foreach (LocalVectorData item in _vectorSource.GetItemsInExtent(_mapCanvas.CurrentExtent))
            {
                if (item.ExpressionId == -1)
                {
                    vStyle = _defaultStyle;
                }
                else
                {
                    if (_layerThemes.Count > item.ExpressionId)
                    {
                        vStyle = _layerThemes[item.ExpressionId].Style;
                    }
                    else
                    {
                        vStyle = _defaultStyle;
                    }
                }
                List<FrameworkElement> elems = _renderer.DrawGeometry(item.Id, item.Geometry, vStyle);
                SetEventsOnElement(elems);
            }
            _isRendering = false;

        }

    }
}
